
//添加内容
var addhtml = ''
addhtml += '<div class="form-div"><form id="fomr" class="layui-form" enctype="multipart/form-data">';
addhtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">名称</label><div class="layui-input-block"><input type="text" name="title" autocomplete="off" class="layui-input" placeholder="请输入商品名称"></div></div>';
addhtml += '<div class="layui-form-item layui-form-select-icon iconfont" style="width:500px;"><label class="layui-form-label">支付货币</label><div class="layui-input-block"><select name="coin_id" lay-ignore class="layui-input cion-type"></select></div></div>';
addhtml += '<div class="layui-form-item layui-form-select-icon iconfont" style="width:500px;"><label class="layui-form-label">分类</label><div class="layui-input-block"><select name="cate_id" lay-ignore class="layui-input"><option value="">请选择商品分类</option><option value="1">重复消费</option><option value="2">提货专区</option></select></div></div>';
addhtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">单价</label><div class="layui-input-block"><input type="text" name="price" autocomplete="off" class="layui-input" placeholder="请输入商品单价"></div></div>';
addhtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">业绩</label><div class="layui-input-block"><input type="text" name="score" autocomplete="off" class="layui-input" placeholder="请输入获得业绩"></div></div>';
addhtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">积分抵扣</label><div class="layui-input-block"><input type="text" name="credit" autocomplete="off" class="layui-input" placeholder="请输入抵扣金额"></div></div>';
addhtml += '<div class="layui-form-item"><label class="layui-form-label">商品描述</label><div class="layui-input-block" id="editor"></div></div>';
addhtml += '<div class="layui-form-item"><label class="layui-form-label">商品图片</label><div class="layui-input-block"><span class="file-bj-bnt layui-btn">上传图片</span><input type="file" id="file" name="image" onchange="uploadsFun()"><div class="image-div"><img src="" /><input id="file-img" type="hidden" name="image"></div></div></div>';
addhtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">单位</label><div class="layui-input-block"><input type="text" name="unit" autocomplete="off" class="layui-input" placeholder="请输入单位"></div></div>';
addhtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">重量(KG)</label><div class="layui-input-block"><input type="text" name="weight" autocomplete="off" class="layui-input" placeholder="请输入单位"></div></div>';
addhtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">库存</label><div class="layui-input-block"><input type="text" name="total" autocomplete="off" class="layui-input" placeholder="请输入单位"></div></div>';
addhtml += '<div class="layui-form-item"><label class="layui-form-label">发货地</label><div class="layui-input-inline layui-form-select-icon iconfont"><select name="" lay-ignore="" class="layui-input cion-type" id="prov" onchange="cityFun($(this))"><option value="">请选择省份</option></select></div><div class="layui-input-inline layui-form-select-icon iconfont"><select lay-ignore="" class="layui-input" id="city" onchange="townFun($(this))"><option value="">请选择城市</option></select></div><div class="layui-input-inline layui-form-select-icon iconfont"><select lay-ignore="" class="layui-input" name="city" id="town"><option value="">请选择县区</option></select></div></div>';
addhtml += '<div class="layui-form-item layui-form-select-icon iconfont" style="width:500px;"><label class="layui-form-label">是否推荐</label><div class="layui-input-block"><select name="is_tj" lay-ignore class="layui-input"><option value="1">是</option><option value="0" selected="selected">否</option></select></div></div>';
addhtml += '<div class="layui-form-item layui-form-select-icon iconfont" style="width:500px;"><label class="layui-form-label">是否复消</label><div class="layui-input-block"><select name="is_fx" lay-ignore class="layui-input"><option value="1">是</option><option value="0" selected="selected">否</option></select></div></div>';
addhtml += '<div class="layui-form-item layui-form-select-icon iconfont" style="width:500px;"><label class="layui-form-label">是否上架</label><div class="layui-input-block"><select name="status" lay-ignore class="layui-input"><option value="1">是</option><option value="0" selected="selected">否</option></select></div></div>';
addhtml += '<input type="hidden" name="token" value="' + token + '">';
addhtml += '</form></div>';

//编辑内容
var edithtml = ''
edithtml += '<div class="form-div"><form id="fomr" class="layui-form">';
edithtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">名称</label><div class="layui-input-block"><input type="text" name="title" autocomplete="off" class="layui-input" placeholder="请输入商品名称"></div></div>';
edithtml += '<div class="layui-form-item layui-form-select-icon iconfont" style="width:500px;"><label class="layui-form-label">支付货币</label><div class="layui-input-block"><select name="coin_id" lay-ignore class="layui-input cion-type"></select></div></div>';
edithtml += '<div class="layui-form-item layui-form-select-icon iconfont" style="width:500px;"><label class="layui-form-label">分类</label><div class="layui-input-block"><select name="cate_id" lay-ignore class="layui-input"><option value="">请选择商品分类</option><option value="1">重复消费</option><option value="2">提货专区</option></select></div></div>';
edithtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">单价</label><div class="layui-input-block"><input type="text" name="price" autocomplete="off" class="layui-input" placeholder="请输入商品单价"></div></div>';
edithtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">业绩</label><div class="layui-input-block"><input type="text" name="score" autocomplete="off" class="layui-input" placeholder="请输入获得业绩"></div></div>';
edithtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">积分抵扣</label><div class="layui-input-block"><input type="text" name="credit" autocomplete="off" class="layui-input" placeholder="请输入抵扣金额"></div></div>';
edithtml += '<div class="layui-form-item"><label class="layui-form-label">商品描述</label><div class="layui-input-block"><textarea name="content" placeholder="请输入内容" class="layui-textarea"></textarea></div></div>';
edithtml += '<div class="layui-form-item"><label class="layui-form-label">商品图片</label><div class="layui-input-block"><span class="file-bj-bnt layui-btn">上传图片</span><input type="file" id="file" name="image" onchange="uploadsFun()"><div class="image-div"><img src="" id="scImg" /><input id="file-img" type="hidden" name="image"></div></div></div>';
edithtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">单位</label><div class="layui-input-block"><input type="text" name="unit" autocomplete="off" class="layui-input" placeholder="请输入单位"></div></div>';
edithtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">重量(KG)</label><div class="layui-input-block"><input type="text" name="weight" autocomplete="off" class="layui-input" placeholder="请输入重量"></div></div>';
edithtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">库存</label><div class="layui-input-block"><input type="text" name="total" autocomplete="off" class="layui-input" placeholder="请输入库存"></div></div>';
edithtml += '<div class="layui-form-item"><label class="layui-form-label">发货地</label><div class="layui-input-inline layui-form-select-icon iconfont"><select name="" lay-ignore="" class="layui-input cion-type" id="prov" onchange="cityFun($(this))"><option value="">请选择省份</option></select></div><div class="layui-input-inline layui-form-select-icon iconfont"><select lay-ignore="" class="layui-input" id="city" onchange="townFun($(this))"><option value="">请选择城市</option></select></div><div class="layui-input-inline layui-form-select-icon iconfont"><select lay-ignore="" class="layui-input" name="city" id="town"><option value="">请选择县区</option></select></div></div>';
edithtml += '<div class="layui-form-item layui-form-select-icon iconfont" style="width:500px;"><label class="layui-form-label">是否推荐</label><div class="layui-input-block"><select name="is_tj" lay-ignore class="layui-input"><option value="1">是</option><option value="0" selected="selected">否</option></select></div></div>';
edithtml += '<div class="layui-form-item layui-form-select-icon iconfont" style="width:500px;"><label class="layui-form-label">是否复消</label><div class="layui-input-block"><select name="is_fx" lay-ignore class="layui-input"><option value="1">是</option><option value="0" selected="selected">否</option></select></div></div>';
edithtml += '<div class="layui-form-item layui-form-select-icon iconfont" style="width:500px;"><label class="layui-form-label">是否上架</label><div class="layui-input-block"><select name="status" lay-ignore class="layui-input"><option value="1">是</option><option value="0" selected="selected">否</option></select></div></div>';
edithtml += '<input type="hidden" name="id">';
edithtml += '<input type="hidden" name="token" value="' + token + '">';
edithtml += '</form></div>';

layui.use('table', function() {
	var table = layui.table,
		form = layui.form;
	//推荐操作
	form.on('switch(tjDemo)', function(obj) {
		var id = this.value;
		if(obj.elem.checked == true) {
			var is_tj = "1"
		} else {
			var is_tj = "0"
		}
		$.ajax({
			type: 'post',
			dataType: 'json',
			url: recSrc,
			data: {
				id: id,
				is_tj: is_tj,
				token:token
			},
			success: function(data) {
				var code = data.code;
				var msg = data.msg;
				if(code == 1) {
					layer.close(layer.index)
					layer.alert(msg, {
						icon: 1
					})
				} else {
					layer.alert(msg, {
						icon: 5
					}, function() {
						location.reload();
					})
				}
			}
		})

	});
	//上下架操作
	form.on('switch(stateDemo)', function(obj) {
		var id = this.value;
		if(obj.elem.checked == true) {
			var status = "1"
		} else {
			var status = "0"
		}
		$.ajax({
			type: 'post',
			dataType: 'json',
			url: recSrc,
			data: {
				id: id,
				status: status,
				token:token
			},
			success: function(data) {
				var code = data.code;
				var msg = data.msg;
				if(code == 1) {
					layer.close(layer.index)
					layer.alert(msg, {
						icon: 1
					})
				} else {
					layer.alert(msg, {
						icon: 5
					}, function() {
						location.reload();
					})
				}
			}
		})
	});
    var $ = layui.$, active = {
    	
    	reload: function() {
			var csLoding=layer.msg('数据加载中...', {
				icon: 16,
				shade: 0.3,
				success: function() {
					var title = $('#title').val();
					var price = $('#price').val();
					var coin_id = $("#coin_id option:selected").val();
					var status = $("#status option:selected").val();
					var is_tj = $('#is_tj').val();
					var start = $('#start').val();
					var end = $('#end').val();
		
					//执行重载
					table.reload('idTest', {
						page: {
							curr: 1 //重新从第 1 页开始
						},
						where: {
							key: {
								title:title,
								price:price,
								coin_id:coin_id,
								status:status,
								is_tj:is_tj,
								start: start,
								end: end
							}
						},
						done: function(){
						     layer.close(csLoding)
						}
					});
				}
			})
			
		}
    }
    $('.right-center #ScreenBnt').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	
	var csLoding=layer.msg('数据加载中...', {
		icon: 16,
		shade: 0.3,
		success: function() {
			table.render({
				elem: '#centet-cen',
				url: listSrc,
				height: $(".center-tab").height(),
				id: 'idTest',
				response: {
		  			statusCode: 1, //成功的状态码，默认：0
				},
				page:true,
				cols: [
					[ //标题栏
						{
							field: 'id',
							title: 'ID',
							width: 60,
							sort: true
						}, {
							field: 'title',
							title: '商品名称'
						}, {
							field: 'price',
							title: '商品价格'
						}, {
							field: 'coin_name',
							title: '支付货币类型'
						}, {
							field: 'unit',
							title: '商品单位'
						}, {
							field: 'total',
							title: '商品库存'
						}, {
							field: 'weight',
							title: '商品重量'
						}, {
							field: 'is_tj',
							title: '是否推荐',
							width: 100,
							templet: '#Recommend',
							unresize: true
						}, {
							field: 'status',
							title: '是否上架',
							width: 100,
							templet: '#switchState',
							unresize: true
						}, {
							field: 'oper',
							title: '操作',
							width: 300,
							minWidth: 160,
							toolbar: '#barDemo'
						}
	
					]
				],
				limit: 10,
				done: function(){
				     layer.close(csLoding)
				}
			});
		}

	})

	table.on('tool(table)', function(obj) {
		var data = obj.data;
		if(obj.event === 'edit') {
			var id = data.id;
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: seeSrc,
				data: {
					id: id
				},
				success: function(data) {
					var address_province = data.address_province;
					var address_citye = data.address_city;
					var address_county = data.address_county;
					var code = data.code;
					var msg = data.msg;
					if(code == 1) {
						layer.open({
							title: "编辑商品",
							type: 1,
							skin: 'layui-layer-rim',
							area: ['800px', '70%'],
							content: edithtml,
							btn: ['提交', '关闭'],
							btn1: function() {
								var Adata = $("#fomr").serialize();
								$.ajax({
									type: 'post',
									dataType: 'json',
									url: editSrc,
									data: Adata,
									success: function(data) {
										var code = data.code;
										var msg = data.msg;
										if(code == 1) {
											layer.close(layer.index)
											layer.alert(msg, {
												icon: 1
											}, function() {
												location.reload();
											})
										} else {
											layer.alert(msg, {
												icon: 5
											})
										}
									}
								})
							}
						});
						$("#fomr select.cion-type").html($("#cionHtml").html());
						$("#scImg").attr("src",data.photo);
						$("#file-img").val(data.photo);
						$("#fomr").formEdit(data);
						form.render();
						$.ajax({
							type: "get",
							url: cryApi,
							success: function(data) {
								listHtml = '<option value="">请选择</option>'
								$.each(data, function(index, value) {
									listHtml += '<option value="' + value.id + '">' + value.name + '</option>';
								});
								$("#prov").html(listHtml);
								for(var i = 0; i < $("#prov option").length; i++) {
									if($("#prov option").eq(i).val() == address_province) {
										$("#prov option").eq(i).attr("selected", "selected");
									}
								}
							}
						});
						$.ajax({
							type: "get",
							url: cryApi,
							data: {
								id: address_province
							},
							success: function(data) {
								listHtml = '<option value="">请选择</option>'
								$.each(data, function(index, value) {
									listHtml += '<option value="' + value.id + '">' + value.name + '</option>';
								});
								$("#city").html(listHtml);
								for(var i = 0; i < $("#city option").length; i++) {
									if($("#city option").eq(i).val() == address_citye) {
										$("#city option").eq(i).attr("selected", "selected");
									}
								}
							}
						});
						$.ajax({
							type: "get",
							url: cryApi,
							data: {
								id: address_citye
							},
							success: function(data) {
								listHtml = '<option value="">请选择</option>'
								$.each(data, function(index, value) {
									listHtml += '<option value="' + value.id + '">' + value.name + '</option>';
								});
								$("#town").html(listHtml);
								for(var i = 0; i < $("#town option").length; i++) {
									if($("#town option").eq(i).val() == address_county) {
										$("#town option").eq(i).attr("selected", "selected");
									}
								}
							}
						});
					} else {
						layer.alert(msg, {
							icon: 5
						})
					}
				}
			})
		} else if(obj.event === 'del') {
			var id = data.id;
			layer.alert('您确定要删除该商品！', {
				icon: 2,
				btn: ['确定', '关闭'],
				btn1: function() {
					$.ajax({
						type: 'post',
						dataType: 'json',
						url: delSrc,
						data: {
							token: token,
							id: id
						},
						success: function(data) {
							var code = data.code;
							var msg = data.msg;
							if(code == 1) {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 1
								}, function() {
									location.reload();
								})
							} else {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 5
								})
							}
						}
					})
				}
			})
		}
	})

})

layui.use(['form', 'layer'], function() {
	var form = layui.form,
		layer = layui.layer;
	var active = {
		add: function() {
			layer.open({
				type: 1,
				title: "添加商品",
				area: ['800px', '70%'], //宽高
				maxmin: true,
				resize: true,
				content: addhtml,
				btn: ['提交', '关闭'],
				btn1: function() {
					var Adata = $("#fomr").serializeArray();
					var edi=editor.txt.html();
	                Adata.push({"name":"content","value":edi});
					$.ajax({
						type: 'post',
						dataType: 'json',
						url: addSrc,
						data: Adata,
						success: function(data) {
							var code = data.code;
							var msg = data.msg;
							if(code == 1) {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 1
								}, function() {
									location.reload();
								})
							} else {
								layer.alert(msg, {
									icon: 5
								})
							}
						}
					})
				}

			});
			var E = window.wangEditor
            var editor = new E('#editor');
            editor.customConfig.uploadImgServer = uploadsImg;
            editor.customConfig.uploadFileName = 'image';
            editor.create()
			provFun();
			$("#fomr select.cion-type").html($("#cionHtml").html());
			form.render();

		}



	}
	$('.right-center').on('click', '.right-btn', function() {
		var othis = $(this),
			method = othis.data('method');
		active[method] ? active[method].call(this, othis) : '';
	});
})

layui.use('laydate', function() {
	var laydate = layui.laydate;
	//开始时间
	laydate.render({
		elem: '#start'
	});
	//结束时间
	laydate.render({
		elem: '#end'
	});

})
layui.use('upload', function(){
  var $ = layui.jquery,upload = layui.upload;

  //普通图片上传
  var uploadInst = upload.render({
    elem: '#test1'
    ,url: '/upload/'
    ,before: function(obj){
      //预读本地文件示例，不支持ie8
      obj.preview(function(index, file, result){
        $('#demo1').attr('src', result); //图片链接（base64）
      });
    }
    ,done: function(res){
      //如果上传失败
      if(res.code > 0){
        return layer.msg('上传失败');
      }
      //上传成功
    }
    ,error: function(){
      //演示失败状态，并实现重传
      var demoText = $('#demoText');
      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
      demoText.find('.demo-reload').on('click', function(){
        uploadInst.upload();
      });
    }
  });
});
$(function() {
	//货币类型
	cionFun();


	$("#Expbnt").click(function() {
		$("#screen-form").attr("action", ExpSrc);
	})



})

function uploadsFun(){
	$.ajax({
			url: uploadsSrc,
			type: 'POST',
			cache: false,
			data: new FormData($('#fomr')[0]),
			processData: false,
			contentType: false,
			success: function(data) {

				if(data.errno==0){
					$('#fomr .image-div img').attr("src",data.data[0])
					$('#file-img').val(data.data[0])
				}


			}
		})
}
